package com.pixiv.controller;

import cn.hutool.json.JSONObject;
import com.pixiv.Entity.AuditTitlePicture;
import com.pixiv.Entity.User;
import com.pixiv.service.AuditTitleService;
import com.pixiv.service.TitleService;
import com.pixiv.tools.ProgramStaticGlobal;
import com.pixiv.tools.SendPack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

@Controller
@RequestMapping("/auditTitle")
public class AuditTitleController {

    @Autowired
    private AuditTitleService auditTitleService;
    @Autowired
    private TitleService titleService;

    @RequestMapping("upload")
    public ModelAndView uploadAuditTitle(){
        return new ModelAndView("pixiv/upload/uploadpicture");
    }

    @ResponseBody
    @RequestMapping("resetSaveTemp")
    public String resetSaveTemp(HttpServletRequest req){
        HttpSession hs = req.getSession();
        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        String n = UUID.randomUUID().toString();

        u.getVar().set_vector("PixivUpPictureStorageServlet_TempPictureID",1);
        u.getVar().set_vector("PixivUpPictureStorageServlet_UPLOAD_ID",n);
        return SendPack.getFlag(1);
    }

    /**
     * 上传用户的临时图片
     * @param file
     * @param req
     */
    @ResponseBody
    @RequestMapping("uploadAuditPicture")
    public String Service_getTempPicture(@RequestParam("file") MultipartFile file, HttpServletRequest req) {

        synchronized(this)
        {
            HttpSession hs = req.getSession();
            User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
            if (!file.isEmpty()) {
                try {

                    String fileName = file.getOriginalFilename();
                    String nameType = fileName.substring(fileName.lastIndexOf(".")).toUpperCase();
                    String path = ProgramStaticGlobal.getPRealPath() + "/user/user_" + u.getUserID() + "/tempStorage";
                    String uuidName = UUID.randomUUID().toString().replace("-", "") + nameType;

                    int count = (int) u.getVar().get_vector("PixivUpPictureStorageServlet_TempPictureID");
                    String uuidTitleID = (String) u.getVar().get_vector("PixivUpPictureStorageServlet_UPLOAD_ID");
                    u.getVar().set_vector("PixivUpPictureStorageServlet_TempPictureID", count + 1);

                    file.transferTo(new File(path, uuidName));
                    auditTitleService.uploadAuditPicture(uuidName, uuidTitleID, count);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            return SendPack.getFlag(1);

        }

    }

    /**
     * 上传图片到特定表内去
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping("uploadToTable")
    public String uploadToTable(HttpServletRequest req)
    {
        return SendPack.getFlag(1);
    }

    @ResponseBody
    @RequestMapping("uploadToAudit")
    public String uploadToAudit(HttpServletRequest req) {
        synchronized(this) {
            HttpSession hs = req.getSession();
            User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");

            String cap = req.getParameter("cap");
            String text = req.getParameter("con");
            String lab = req.getParameter("lab");
            int ageLimit = Integer.parseInt(req.getParameter("age"));
            int orgin = Integer.parseInt(req.getParameter("or"));

            String uuid = (String) u.getVar().get_vector("PixivUpPictureStorageServlet_UPLOAD_ID");

            HashMap<String, Object> hm = new HashMap();
            hm.put("UUID", uuid);
            hm.put("Caption", cap);
            hm.put("Context", text);
            hm.put("LabelInformation", lab);
            hm.put("AgeLimit", ageLimit);
            hm.put("Orginal", orgin);
            hm.put("UserID", u.getUserID());
            auditTitleService.uploadAuditPictureTitle(hm);
            return SendPack.getFlag(1);
        }
    }

    @ResponseBody
    @RequestMapping("initImage")
    public String initImage(HttpServletRequest req, HttpServletResponse resp)
    {
        HttpSession hs = req.getSession();
        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        AuditTitlePicture auditTitlePicture = (AuditTitlePicture) u.getVar().get_vector("GotoAuditObjectTargetPictureObj");
        //Long count = auditTitlePicture.getPictureTotal();
        int count = auditTitleService.getPicturesTotal(auditTitlePicture.getAuditTitleUUID());
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("flag",1);
        jsonObject.accumulate("count",count);

        return jsonObject.toString();
    }

    @RequestMapping("getImage")
    public void getImage(HttpServletRequest req, HttpServletResponse resp)
    {
        //System.out.println(id);
        HttpSession hs = req.getSession();
        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        AuditTitlePicture auditTitlePicture = (AuditTitlePicture) u.getVar().get_vector("GotoAuditObjectTargetPictureObj");
        int location = Integer.parseInt(req.getParameter("id"));
        String uuid = auditTitlePicture.getAuditTitleUUID();

        String path = auditTitleService.getPicture(uuid,location);
        path = "user\\user_" + auditTitlePicture.getUserid() + "\\tempStorage\\" + path;
        //System.out.println(path);
        //System.out.println(path);
        User.outPutPictureStream(resp,path,null,null);

    }

    @ResponseBody
    @RequestMapping("ApprovedTitle")
    public String approvedTtile(HttpServletRequest req, HttpServletResponse resp)
    {
        HttpSession hs = req.getSession();
        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        AuditTitlePicture auditTitlePicture = (AuditTitlePicture) u.getVar().get_vector("GotoAuditObjectTargetPictureObj");
        auditTitlePicture.approvedTitle(titleService);
        auditTitlePicture.approvedPicture(titleService);
        auditTitlePicture.deleteAuditData(auditTitleService);
        u.getVar().set_vector("GotoAuditObjectTargetPictureObj",null);

        return SendPack.getFlag(1);

    }

}
